import json


def load_data(file_path):
    with open(file_path, 'r') as f:
        data = json.load(f)
    return data


def validate_ab_fractals(data):
    i = 0
    while i < len(data) - 1:
        if data[i]['top_fractal'] or data[i]['bottom_fractal']:
            for j in range(i + 1, len(data)):
                if data[j]['top_fractal'] or data[j]['bottom_fractal']:
                    interval = j - i - 1
                    if interval < 3:
                        data[i]['top_fractal'] = False
                        data[i]['bottom_fractal'] = False
                        data[j]['top_fractal'] = False
                        data[j]['bottom_fractal'] = False
                    break
        i += 1
    return data


def save_data(data, output_file):
    with open(output_file, 'w') as f:
        json.dump(data, f, indent=4)


if __name__ == "__main__":
    input_file = '../static/final_processed_bnb_usdt_klines.json'
    output_file = '../static/validated_processed_bnb_usdt_klines.json'

    kline_data = load_data(input_file)
    validated_data = validate_ab_fractals(kline_data)
    save_data(validated_data, output_file)

    print(f"Validated processed fractals have been saved to {output_file}")
